<template>
	<view>
		<cu-custom bgColor="bg-red" :isBack="true">		<block slot="backText"></block>
			<block slot="content">拼购管理</block>
		</cu-custom>
		<scroll-view scroll-x class="bg-white nav text-center">
			<view class="cu-item" v-for="(item,index) in listObj" :key="index" :class="index==TabCur?'cur text-'+theme:'text-gray'"
			 @tap="tabSelect" :data-id="index">
				{{item.title}}
			</view>
		</scroll-view>

		<view v-for="(item,index) in listObj" :key="index" v-if="index==TabCur" class="">
			<block v-for="(item2 ,i) in item.list" :key="i">
				<block v-if="index==0">
					<view class="margin-bottom padding bg-white" @tap="goPage('/pages/member/wholesale/detail?id='+item2.id)">
						<view class="space-between">
							<view class="">发起时间：{{$common.timeFormat(item2.created_at)}}</view>
							<view class="text-bold">
								<text v-if="item2.state == 1">待开奖</text>
								<text v-if="item2.state == 2">已开奖</text>
								<text v-if="item2.state == 3">失败</text>
							</view>
						</view>
						<view class="space-between margin-top solid-top padding-top">
							<view class="cu-avatar lg bg-white">
								<image :src="item2.product?item2.product.picture:''" mode="aspectFill"></image>
							</view>
							<view class="w100 margin-left">
								<view class="text-overflow">{{item2.product?item2.product.name:''}}</view>
								<view class="space-between">
									<view class="text-price text-orange margin-top">{{item2.product?item2.product.wholesale_price:''}}</view>
									<view class="cu-btn round sm line-gray">查看详情</view>
								</view>
							</view>
						</view>
					</view>
				</block>
				<block v-if="index==1">
					<view class="margin-bottom padding bg-white" @tap="goPage('/pages/member/wholesale/detail?id='+item2.wholesale.id)">
						<view class="space-between">
							<view class="">参与时间：{{$common.timeFormat(item2.created_at)}}</view>
							<view class="text-bold">
								<text v-if="item2.wholesale.state == 1">待开奖</text>
								<text v-if="item2.wholesale.state == 2">
									<block v-if="item2.is_win == 1">已中奖</block>
									<block v-else>未中奖</block>
								</text>
								<text v-if="item2.wholesale.state == 3">失败</text>
							</view>
						</view>
						<view class="space-between margin-top solid-top padding-top" v-if="item2.wholesale.wholesaleProduct">
							<view class="cu-avatar lg bg-white">
								<image :src="item2.wholesale.wholesaleProduct.type==0?item2.wholesale.product.picture:item2.wholesale.wholesaleProduct.picture" mode="aspectFill"></image>
							</view>
							<view class="w100 margin-left">
								<view class="text-overflow">{{item2.wholesale.wholesaleProduct.type==0?item2.wholesale.product.name:item2.wholesale.wholesaleProduct.title}}</view>
								<view class="space-between">
									<view class="text-price text-orange margin-top">{{item2.wholesale.wholesaleProduct.type==0?item2.wholesale.product.wholesale_price:item2.wholesale.wholesaleProduct.wholesale_price}}</view>
									<view class="cu-btn round sm line-gray">查看详情</view>
								</view>
							</view>
						</view>
					</view>
				</block>
			</block>
			<view class="cu-load margin-center" :class="item.loading?'loading':'over'"></view>
		</view>
		<Navbar ref="navbar" PageCur="wholesale"></Navbar>
	</view>
</template>

<script>
	import Navbar from '@/components/default/Navbar.vue'
	export default {
		components: {
			Navbar
			},
		data() {
			return {
				theme: this.$config.THEME(),
				TabCur: 0,
				is_channel: 0,
				listObj: [{
						title: '我的发起',
						list: [],
						hasMore: true,
						loading: false,
						page: 1,
						type: 0
					},
					{
						title: '我的参与',
						list: [],
						hasMore: true,
						loading: false,
						page: 1,
						type: 1
					}
				],
			};
		},
		onLoad(options) {
			if (this.$common.loginAuth(true)) {
				this.$nextTick(function() {
					uni.startPullDownRefresh({})
				})
			}
		},
		onShow() {
			if (this.$common.loginAuth(true)) {}
		},
		methods: {
			tabSelect(e) {
				if (this.TabCur != e.currentTarget.dataset.id) {
					this.TabCur = e.currentTarget.dataset.id
					this.listObj[this.TabCur].list = []
					this.listObj[this.TabCur].hasMore = true
					this.listObj[this.TabCur].page = 1
					this.listObj[this.TabCur].loading = false
					this.getList(this.TabCur)
				}
			},
			getList(index) {
				let obj = this.listObj[index]
				if (!obj.hasMore || obj.loading) return

				this.listObj[index].loading = true
				let params = {
					page: obj.page,
					type: obj.type,
				}
				this.$http
					.get('marketing_wholesale_log', params)
					.then((response) => {
						let list = obj.list
						if (response.data.data.length > 0) {
							list = list.concat(response.data.data)
						} else {
							this.listObj[index].hasMore = false
						}
						this.listObj[index].list = list
						this.listObj[index].loading = false
						this.listObj[index].page++
						uni.stopPullDownRefresh()
					})
					.catch((response) => {
						console.log(response)
					})
			},
			reloadList() {
				uni.startPullDownRefresh({})
			}
		},
		onPullDownRefresh() {
			this.listObj[this.TabCur].list = []
			this.listObj[this.TabCur].hasMore = true
			this.listObj[this.TabCur].page = 1
			this.listObj[this.TabCur].loading = false
			this.getList(this.TabCur)
		},
		onReachBottom() {
			this.getList(this.TabCur)
		},
	}
</script>

<style>
</style>
